﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 天然气，公共方法
    /// </summary>
    /// 创建时间：2023-1-17 10:29:41。作者：王浩力
    internal class GasCommonMethod
    {
        /// <summary>
        /// 导入指标文本文件，处理。
        /// </summary>
        /// <param name="window"></param>
        /// <param name="e"></param>
        /// 创建时间：2023-1-17 10:29:41。作者：王浩力
        public static void ImportTxtReadValue(Window window, RoutedEventArgs e)
        {
            //ShowAlertHelpter.ShowAlert("导入文件",this);
            var openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Filter = "Text documents (.txt)|*.txt";
            openFileDialog.Title = "选择指标导入文件";
            var result = openFileDialog.ShowDialog();
            if (result == false)
            {
                e.Handled = true;
                return;
            }
            try
            {
                //解析文本，读取指数
                string content = System.IO.File.ReadAllText(openFileDialog.FileName);
                int lastIndex = content.LastIndexOf("CalibPeakType");
                content = content.Substring(lastIndex);
                content = content.Substring(content.LastIndexOf("Compound") + 10);

                var match = Regex.Replace(content.Trim(), "[-+]", "");
                var arr = match.Split(new[] { "TCD2B", "TCD1A" }, StringSplitOptions.RemoveEmptyEntries);
                var list = arr.ToList();
                if (arr.Length > 0)
                {
                    list.RemoveAt(0);
                }
                Dictionary<string, string> dict = new Dictionary<string, string>();
                foreach (var item in list)
                {
                    string valueItem = item.Split("\r\n", StringSplitOptions.RemoveEmptyEntries)[0];
                    var dv = valueItem.Split("%", StringSplitOptions.RemoveEmptyEntries);
                    dict.TryAdd(dv[1].Trim(), dv[0].Trim());
                }

                //输入框载入导入值
                foreach (var item in dict)
                {
                    string name = item.Key.ToUpper();
                    if (name.Equals("CH4"))
                    {
                        name = "C1";
                    }
                    var elem = window.FindName(name);
                    if (elem != null)
                    {
                        //处理保留的小数位，添加时间：2023-2-7 13:58:28。作者：王浩力
                        //(elem as TextBox).Text = item.Value;
                        (elem as TextBox).Text = item.Value.ToKeepDecimal(4);
                    }
                }
 
            }
            catch (Exception ex)
            {
                ShowAlertHelpter.ShowAlert("无效文件");
            }
        }

        /// <summary>
        /// 计算总值
        /// </summary>
        /// 创建时间：2023-1-17 10:29:41。作者：王浩力
        public static void CalcTotalValue(Window window)
        {
            //计算总值        
            decimal? totalSum =
                  +(window.FindName("HE") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("H2") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("O2") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("N2") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("C1") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("C6") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("C3") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("IC4") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("NC4") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("IC5") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("NC5") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("CO2") as TextBox)?.Text.ToMyDecimal2()
                  + (window.FindName("C2") as TextBox)?.Text.ToMyDecimal2();  

            //总值
            (window.FindName("txt_totalSum") as TextBox).Text = totalSum.ToString();
        }


    }
}
